package br.com.felix.fwt.svg;

import java.io.PrintWriter;
import java.util.ArrayList;
import java.util.List;

import br.com.felix.fwt.svg.group.SVGGroup;
import br.com.felix.fwt.svg.transform.SVGTransform;

public class SVGDocument implements SVG {

	private int x, y, width, height;

	private List<SVG> children;

	private boolean invertVerticalCoordinateSystem; 
	
	/**
	 * (min x, min y, width, height)
	 * */
	private SVGViewBox viewBox;



	public SVGDocument() {
		children = new ArrayList<SVG>();
	}



	public void add(SVG ...svgs) {
		for(SVG svg:svgs){
			children.add(svg);
		}
	}



	@Override
	public void toSVG(SVGBuilder svgBuilder) {
		svgBuilder.declare(viewBox, width, height);
		if(invertVerticalCoordinateSystem){
			SVGGroup svgGroup = new SVGGroup();
			SVGTransform svgTransform = new SVGTransform();
			svgTransform.matrix(1, 0, width, 0, -1, height);
			svgGroup.transform(svgTransform);
			svgGroup.addAll(children);
			svgGroup.toSVG(svgBuilder);
		}
		else{
			for (SVG child : children) {
				child.toSVG(svgBuilder);
			}
		}
		svgBuilder.endDeclaration();
	}



	public int getWidth() {
		return width;
	}



	public int getHeight() {
		return height;
	}



	public String toString() {
		SVGBuilder svgBuilder = new SVGBuilder();
		toSVG(svgBuilder);
		return svgBuilder.toString();
	}


	public void write(PrintWriter printWriter){
		SVGBuilder svgBuilder = new SVGBuilder(printWriter);
		toSVG(svgBuilder);
	}

	
	public SVGDocument width(int width) {
		this.width = width;
		return this;
	}



	public SVGDocument height(int height) {
		this.height = height;
		return this;
	}



	public boolean getInvertVerticalCoordinateSystem() {
		return invertVerticalCoordinateSystem;
	}



	public void setInvertVerticalCoordinateSystem(boolean invertVerticalCoordinateSystem) {
		this.invertVerticalCoordinateSystem = invertVerticalCoordinateSystem;
	}

}
